/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Server;

/**
 *
 * @author spok
 */

import java.awt.Point;
import plar.Elements.Player;
import java.util.*;
import plar.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class Server {
    
    public HashMap info;
    public Level level;
    public Vector <Player> players;
    public String xchat;
    
    public Server (Level l)
    {
        level = l;
    }
    
    public Player addPlayer(Resource p)
    {
        return (Player)level.addElement(new Player((String)p.properties.get("name")));
    }
  public Player addPlayer(String p)
    {
        return (Player)level.addElement(new Player(p));
    }

    public Vector <Resource> possiblePlayer()
    {
        Vector <Resource> a = new Vector <Resource> ();
        Resource p = new Resource();
        p.properties.put("name", "Player");
        p.properties.put("image", "keen.png");
        p.properties.put("description", "test player");
        
        a.add(p);
        return a;
    }
    
    public Resource info()
    {
        
        Resource p = new Resource();
        p.properties.put("version", "0.1");
        return p;
    }
      
    public Resource getResources()
    {
        
        Resource p = new Resource();
        p.properties.put("block.png", "1234");
        p.properties.put("elevator.png", "1234");
        p.properties.put("gun.png", "1234");
        p.properties.put("icecream0.png", "1234");
        p.properties.put("icecream1.png", "1234");
        p.properties.put("platform.png", "1234");
        p.properties.put("respawn.png", "1234");
        return p;
    }
      

    
    public static byte[] createChecksum(String filename) throws
       Exception{
     InputStream fis =  new FileInputStream(filename);

     byte[] buffer = new byte[1024];
     MessageDigest complete = MessageDigest.getInstance("MD5");
     int numRead;
     do {
      numRead = fis.read(buffer);
      if (numRead > 0) {
        complete.update(buffer, 0, numRead);
        }
      } while (numRead != -1);
     fis.close();
     return complete.digest();
   }


    public Resource movePlayer(Player up,UserInput i)
    {
        level.userInput(up,i);
        
        Resource p = new Resource();
        p.properties.put("state", "ok");
        return p;
    }
    
    public String chat(String user,String x)
    {
      return xchat+="["+user+"] "+x+"\n";
    }
    
    public String chat()
    {
      return xchat;
    }
    
    public Resource  login(String user,String password)
    {
         Resource p = new Resource();
         level.addElement(new Player(user));
        p.properties.put("state", "ok");
        return p;
    }
    public  Vector <Element> getScreen(Element p)
    {
      return level.showScreen(p);
    }
    
    public Resource getState(Element e)
    {
        Vector <Element> se = level.showScreen(e);
         Point epos = (Point)e.properties.get("position");
         int i = 0;
         Resource p = new Resource();
         for(Element e0:se)
         {
             i++;
          Point e0pos = new Point();
          e0pos.x=((Point)e0.properties.get("position")).x-((Point)e.properties.get("position")).x;
          e0pos.y=((Point)e0.properties.get("position")).y-((Point)e.properties.get("position")).y;
          p.properties.put("e"+i, "block.png;"+e0pos.x+";"+e0pos.y);
         }
             
        p.properties.put("state", "ok");
        return p;
    } 
}
